<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>邀请码管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #283c86 0%, #45a247 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 800px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }

        header {
            background: #2c3e50;
            color: white;
            padding: 20px;
            text-align: center;
        }

        h1 {
            font-size: 28px;
            margin-bottom: 5px;
        }

        .subtitle {
            font-size: 14px;
            opacity: 0.8;
        }

        .main-content {
            padding: 30px;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }

        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
            }
        }

        .card {
            background: white;
            border-radius: 8px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .card-title {
            font-size: 20px;
            color: #2c3e50;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #ecf0f1;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #7f8c8d;
            font-weight: 500;
        }

        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        .btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            transition: all 0.3s;
            width: 100%;
        }

        .btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }

        .btn-generate {
            background: #2ecc71;
        }

        .btn-generate:hover {
            background: #27ae60;
        }

        .result-card {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            background: #f8f9fa;
            display: none;
        }

        .success {
            border-left: 4px solid #2ecc71;
        }

        .error {
            border-left: 4px solid #e74c3c;
        }

        .result-title {
            font-weight: 600;
            margin-bottom: 10px;
        }

        .code-value {
            font-family: monospace;
            font-size: 18px;
            color: #2c3e50;
            word-break: break-all;
        }

        .code-list {
            max-height: 300px;
            overflow-y: auto;
        }

        .code-item {
            padding: 12px 15px;
            background: #f8f9fa;
            border-radius: 5px;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-left: 4px solid #3498db;
        }

        .code-info {
            flex-grow: 1;
        }

        .code-status {
            color: #7f8c8d;
            font-size: 14px;
        }

        .stat {
            display: flex;
            justify-content: space-between;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1>邀请码管理系统</h1>
        <div class="subtitle">管理员专用界面</div>
    </header>

    <div class="main-content">
        <div class="card">
            <h3 class="card-title">生成邀请码</h3>
            <div class="form-group">
                <label for="password">验证口令</label>
                <input type="password" id="password" placeholder="请输入生成口令">
            </div>
            <div class="form-group">
                <label for="creatorId">创建者ID</label>
                <input type="number" id="creatorId" placeholder="请输入管理员ID">
            </div>
            <button class="btn btn-generate" onclick="generateInviteCode()">生成邀请码</button>

            <div class="result-card" id="resultContainer">
                <div class="result-title">生成结果：</div>
                <div id="resultContent"></div>
            </div>
        </div>

        <div class="card">
            <h3 class="card-title">最近生成的邀请码</h3>
            <div class="invite-codes">
                <div class="code-list" id="code-list">
                    <!-- 动态生成的邀请码列表 -->
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 模拟历史邀请码数据（实际应用中从后端API获取）
    const mockHistoryCodes = [

    ];

    // 页面加载时初始化历史记录
    document.addEventListener('DOMContentLoaded', () => {
        renderCodeHistory(mockHistoryCodes);
    });

    // 渲染历史邀请码
    function renderCodeHistory(codes) {
        const container = document.getElementById('code-list');
        container.innerHTML = '';

        codes.forEach(item => {
            const codeItem = document.createElement('div');
            codeItem.className = 'code-item';

            codeItem.innerHTML = `
                    <div class="code-info">
                        <div class="code-value">${item.code}</div>
                        <div class="stat">
                            <span class="code-status">创建者: ${item.creatorId}</span>
                            <span class="code-status">${formatDate(item.createdAt)}</span>
                        </div>
                    </div>
                    <span class="code-status" style="color:${item.used ? '#e74c3c' : '#2ecc71'}">
                        ${item.used ? '已使用' : '未使用'}
                    </span>
                `;

            container.appendChild(codeItem);
        });
    }

    // 日期格式化
    function formatDate(dateString) {
        const date = new Date(dateString);
        return `${date.getFullYear()}-${pad(date.getMonth()+1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
    }

    function pad(num) {
        return num.toString().padStart(2, '0');
    }

    // 生成邀请码
    async function generateInviteCode() {
        const password = document.getElementById('password').value;
        const creatorId = document.getElementById('creatorId').value;
        const resultContainer = document.getElementById('resultContainer');
        const resultContent = document.getElementById('resultContent');

        // 清空结果
        resultContainer.className = 'result-card';
        resultContent.innerHTML = '';

        // 验证输入
        if (!password || !creatorId) {
            showError('请填写口令和创建者ID');
            return;
        }

        try {
            // 调用后端API（替换为实际API地址）
            const response = await fetch(`/api/invite-codes?password=${encodeURIComponent(password)}&creatorId=${creatorId}`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' } // 请求体可为空
            });

            const data = await response.json();

            if (response.ok) {
                // 显示成功结果
                resultContainer.className = 'result-card success';
                resultContent.innerHTML = `
                        <div class="code-value">${data.code}</div>
                        <div class="stat">
                            <span class="code-status">状态: 未使用</span>
                            <span class="code-status">创建时间: ${formatDate(data.createdAt)}</span>
                        </div>
                        <div class="stat">
                            <span class="code-status">创建者ID: ${data.creatorId}</span>
                        </div>
                    `;

                // 添加到历史列表顶部
                mockHistoryCodes.unshift({
                    code: data.code,
                    creatorId: data.creatorId,
                    createdAt: data.createdAt,
                    used: false
                });
                renderCodeHistory(mockHistoryCodes);

            } else {
                // 显示API错误
                showError(data.message );
            }

        } catch (error) {
            showError('网络请求失败: ' + error.message);
        }
    }

    // 显示错误信息
    function showError(message) {
        const resultContainer = document.getElementById('resultContainer');
        const resultContent = document.getElementById('resultContent');

        resultContainer.className = 'result-card error';
        resultContent.innerHTML = `<div style="color:#e74c3c">❌ ${message}</div>`;
        resultContainer.style.display = 'block';
    }
</script>
</body>
</html>